package it.univpm.diiga.memorysim.kernel;

import it.univpm.diiga.memorysim.kernel.shared.EmptyBlock;
import it.univpm.diiga.memorysim.kernel.shared.Parameters;
import it.univpm.diiga.memorysim.kernel.shared.ProcessRequest;

public class Swapper {

	private ProcessRequest pageToDelete;
	private ProcessRequest pageToInsert;
	private int start = 0;
	
	public Swapper(ProcessRequest pageToDelete,ProcessRequest pageToInsert){
		this.pageToDelete = pageToDelete;
		this.pageToInsert = pageToInsert;
	}
	
	public void swap(){
		if(checkPageInSwap(pageToDelete)){
			int startInsert = pageToInsert.getStart();
			int startDelete = pageToDelete.getStart();
			int endInsert = pageToInsert.getEnd();
			int endDelete = pageToDelete.getEnd();
			
			int indexInsert = Parameters.getProcessMemory().lastIndexOf(pageToInsert);
			int indexDelete = Parameters.getSwap().lastIndexOf(pageToDelete);
			
			ProcessRequest p = new ProcessRequest(pageToInsert.getPID(),pageToInsert.getPagina(),pageToInsert.getOffset(),pageToInsert.getDimProcess(),pageToInsert.getDurata(), false);
			p.setStart(startDelete);
			p.setEnd(endDelete);
			//pageToDelete.setStart(startInsert);
			//pageToDelete.setEnd(endInsert);
			//Parameters.getProcessMemory().remove(indexInsert);
			//Parameters.getProcessMemory().add(indexInsert, pageToDelete);
			Parameters.getSwap().remove(indexDelete);
			Parameters.getSwap().add(indexDelete, p);
		}
		else{
			if(Parameters.getSwap().size() == 0)
				start = 0;
			else
				start = Parameters.getSwap().get(Parameters.getSwap().size()-1).getEnd()+1;
			pageToInsert.setStart(start);
			pageToInsert.setEnd(start + pageToInsert.getDimBlock() - 1);
			Parameters.getSwap().add(pageToInsert);
			start +=  pageToInsert.getDimBlock();
		}
		
		System.out.println("----SWAP----");
		for(int i=0; i<Parameters.getSwap().size();i++){
			System.out.println(Parameters.getSwap().get(i));
			System.out.println("");
		}
		System.out.println("----FINE SWAP----");
	}
	
	/*public void swap(){
		if(checkPageInSwap(pageToDelete)){
			int PID = pageToDelete.getPID();
			int page= pageToDelete.getPagina();
			int indexPage = -1;
			int start1 =0;
			int end = 0;
			for(int i=0;i<Parameters.getSwap().size();i++){
				int actualPage = ((ProcessRequest) Parameters.getSwap().get(i)).getPagina();
				int actualPID = ((ProcessRequest) Parameters.getSwap().get(i)).getPID();
				if(PID == actualPID && page == actualPage){
					indexPage=i;
					start1 =((ProcessRequest) Parameters.getSwap().get(i)).getStart();
					end = ((ProcessRequest) Parameters.getSwap().get(i)).getEnd();
					System.out.println(((ProcessRequest) Parameters.getSwap().get(i)));
				}
			}
			Parameters.getSwap().remove(indexPage);
			pageToInsert.setStart(start1);
			pageToInsert.setEnd(end);
			Parameters.getSwap().add(indexPage, pageToInsert);
		}
		else{
			if(Parameters.getSwap().size() == 0)
				start = 0;
			else
				start = Parameters.getSwap().get(Parameters.getSwap().size()-1).getEnd()+1;
			pageToInsert.setStart(start);
			pageToInsert.setEnd(start + pageToInsert.getDimBlock() - 1);
			Parameters.getSwap().add(pageToInsert);
			start +=  pageToInsert.getDimBlock();
		}
		
		System.out.println("----SWAP----");
		for(int i=0; i<Parameters.getSwap().size();i++){
			System.out.println(Parameters.getSwap().get(i));
			System.out.println("");
		}
		System.out.println("----FINE SWAP----");
		
	}*/
		
	private boolean checkPageInSwap(ProcessRequest proc){
		if(Parameters.getSwap().size() == 0){
			System.out.println("PAGINA NON PRESENTE IN SWAP --- CARICATA DALL'HD");
			return false;
		}
		boolean exist = false;
		int PID = proc.getPID();
		int page= proc.getPagina();
		for(int i=0;i<Parameters.getSwap().size();i++){
			int actualPage = ((ProcessRequest) Parameters.getSwap().get(i)).getPagina();
			int actualPID = ((ProcessRequest) Parameters.getSwap().get(i)).getPID();
			if(PID == actualPID && page == actualPage){
				exist = true;
			}
		}
		if(exist){
			System.out.println("PAGINA PRESENTE IN SWAP");
			return true;	
		}
		else{
			System.out.println("PAGINA NON PRESENTE IN SWAP --- CARICATA DALL'HD");
			return false;
		}
	}	
}
